package com.huihuiyun.huihuiyun.server.service;

import com.huihuiyun.huihuiyun.server.domain.HhyWallet;
import com.huihuiyun.huihuiyun.server.repository.HhyWalletRepository;
import com.mark.util.CommonUtil;
import com.mark.util.JaxrsUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.List;

@Component
@Path("/hhyWallets")
@Slf4j
public class HhyWalletService {
    @Autowired
    private HhyWalletRepository hhyWalletRepository;
    @Context
    private HttpServletResponse httpServletResponse;

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public HhyWallet post(HhyWallet hhyWallet) {
        try {
            hhyWallet.id = CommonUtil.createRandomId();
            hhyWallet = hhyWalletRepository.save(hhyWallet);
            JaxrsUtil.setHttpServletResponseStatusCode(httpServletResponse, Response.Status.CREATED.getStatusCode());
            return hhyWallet;
        } catch (Exception e) {
            log.error("", e);
            JaxrsUtil.setHttpServletResponseStatusCode(httpServletResponse, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
            return null;
        }
    }

    @DELETE
    @Path("/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    public void delete(@PathParam("id") String id) {
        try {
            hhyWalletRepository.delete(id);
            JaxrsUtil.setHttpServletResponseStatusCode(httpServletResponse, Response.Status.NO_CONTENT.getStatusCode());
        } catch (Exception e) {
            log.error("", e);
            JaxrsUtil.setHttpServletResponseStatusCode(httpServletResponse, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
        }
    }

    @PUT
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public HhyWallet put(HhyWallet hhyWallet) {
        try {
            if (CommonUtil.isEmpty(hhyWallet.id)) {
                hhyWallet.id = CommonUtil.createRandomId();
            }
            hhyWallet = hhyWalletRepository.save(hhyWallet);
            return hhyWallet;
        } catch (Exception e) {
            log.error("", e);
            JaxrsUtil.setHttpServletResponseStatusCode(httpServletResponse, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
            return null;
        }
    }

    @GET
    @Path("/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    public HhyWallet findOne(@PathParam("id") String id) {
        try {
            HhyWallet hhyWallet = hhyWalletRepository.findOne(id);
            if (hhyWallet == null) {
                JaxrsUtil.setHttpServletResponseStatusCode(httpServletResponse, Response.Status.NO_CONTENT.getStatusCode());
                return null;
            } else {
                return hhyWallet;
            }
        } catch (Exception e) {
            log.error("", e);
            JaxrsUtil.setHttpServletResponseStatusCode(httpServletResponse, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
            return null;
        }
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public List<HhyWallet> findAll() {
        try {
            List<HhyWallet> hhyWalletList = hhyWalletRepository.findAll();
            return hhyWalletList;
        } catch (Exception e) {
            log.error("", e);
            JaxrsUtil.setHttpServletResponseStatusCode(httpServletResponse, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
            return null;
        }
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/findAll")
    public Page<HhyWallet> findAll(@QueryParam("pageNumber") int pageNumber, @QueryParam("pageSize") int pageSize) {
        try {
            PageRequest pageRequest = new PageRequest(pageNumber, pageSize);
            Page<HhyWallet> page = hhyWalletRepository.findAll(pageRequest);
            return page;
        } catch (Exception e) {
            log.error("", e);
            JaxrsUtil.setHttpServletResponseStatusCode(httpServletResponse, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
            return null;
        }
    }

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/branch")
    public void post(List<HhyWallet> hhyWalletList) {
        try {
            hhyWalletRepository.save(hhyWalletList);
        } catch (Exception e) {
            log.error("", e);
            JaxrsUtil.setHttpServletResponseStatusCode(httpServletResponse, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
        }
    }

    /**
     * 获取HhyWallet， 不存在就创建
     *
     * @param hhyWalletId
     * @return
     */
    public HhyWallet findIf(String hhyWalletId) {
        try {
            HhyWallet hhyWallet = findOne(hhyWalletId);
            if (hhyWallet == null) {
                hhyWallet = new HhyWallet();
                hhyWallet.id = hhyWalletId;
                hhyWallet = hhyWalletRepository.save(hhyWallet);
            }
            return hhyWallet;
        } catch (Exception e) {
            log.error("", e);
            JaxrsUtil.setHttpServletResponseStatusCode(httpServletResponse, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
            return null;
        }
    }

    public HhyWallet findIfByPhone(String phone) {
        try {
            HhyWallet hhyWallet = hhyWalletRepository.findHhyWalletByPhone(phone);
            if (hhyWallet == null) {
                hhyWallet = new HhyWallet();
                hhyWallet.id = CommonUtil.createRandomId();
                hhyWallet.phone = phone;
                hhyWallet = hhyWalletRepository.save(hhyWallet);
            }
            return hhyWallet;
        } catch (Exception e) {
            log.error("", e);
            JaxrsUtil.setHttpServletResponseStatusCode(httpServletResponse, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
            return null;
        }
    }

    public void transferPoints(String hhyWalletIdFrom, String hhyWalletIdTo, long points) {
        try {
            HhyWallet hhyWalletFrom = findIf(hhyWalletIdFrom);
            HhyWallet hhyWalletTo = findIf(hhyWalletIdTo);
            hhyWalletFrom.enableWithdrawLimitPoints = hhyWalletFrom.enableWithdrawLimitPoints - points;
            hhyWalletTo.enableWithdrawLimitPoints = hhyWalletTo.enableWithdrawLimitPoints + points;

            hhyWalletRepository.save(hhyWalletFrom);
            hhyWalletRepository.save(hhyWalletTo);
        } catch (Exception e) {
            log.error("", e);
            JaxrsUtil.setHttpServletResponseStatusCode(httpServletResponse, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
        }
    }

    public void addMoney(String hhyWalletId, long money) {
        try {
            HhyWallet hhyWallet = findIf(hhyWalletId);
            hhyWallet.cash = hhyWallet.cash + money;
            hhyWalletRepository.save(hhyWallet);
        } catch (Exception e) {
            log.error("", e);
            JaxrsUtil.setHttpServletResponseStatusCode(httpServletResponse, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
        }
    }

    public void subMoney(String hhyWalletId, long money) {
        try {
            HhyWallet hhyWallet = findIf(hhyWalletId);
            hhyWallet.cash = hhyWallet.cash - money;
            hhyWalletRepository.save(hhyWallet);
        } catch (Exception e) {
            log.error("", e);
            JaxrsUtil.setHttpServletResponseStatusCode(httpServletResponse, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
        }
    }

    public void addPoints(String hhyWalletId, long points) {
        try {
            HhyWallet hhyWallet = findIf(hhyWalletId);
            hhyWallet.enableWithdrawLimitPoints = hhyWallet.enableWithdrawLimitPoints + points;
            hhyWalletRepository.save(hhyWallet);
        } catch (Exception e) {
            log.error("", e);
            JaxrsUtil.setHttpServletResponseStatusCode(httpServletResponse, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
        }
    }

    public HhyWallet withdrawByWalletIdAndWithdrawPoints(String hhyWalletId, long withdrawPoints) {
        try {
            HhyWallet hhyWallet = findIf(hhyWalletId);
            hhyWallet.enableWithdrawLimitPoints = hhyWallet.enableWithdrawLimitPoints - withdrawPoints;
            hhyWallet.freezePoints = hhyWallet.freezePoints + withdrawPoints;
            hhyWallet = hhyWalletRepository.save(hhyWallet);
            return hhyWallet;
        } catch (Exception e) {
            log.error("", e);
            JaxrsUtil.setHttpServletResponseStatusCode(httpServletResponse, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
            return null;
        }
    }
}
